package yxy.cherry.game.base.bean;

import org.redisson.api.RBucket;

import yxy.apple.util.RandomStringUtility;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;

public class ChatBean {
	private String innertoken;
	private int system;
	private int cluster;

	public String getInnertoken() {
		return innertoken;
	}

	public void setInnertoken(String innertoken) {
		this.innertoken = innertoken;
	}

	public int getSystem() {
		return system;
	}

	public void setSystem(int system) {
		this.system = system;
	}

	public int getCluster() {
		return cluster;
	}

	public void setCluster(int cluster) {
		this.cluster = cluster;
	}

	static private String key = "chat_inner";

	private static RBucket<ChatBean> touch(RedisConnection redis) throws RedisException {
		if (redis.ready()) {
			try {
				return redis.client().getBucket(key);
			} catch (Exception e) {
				throw new RedisException(e);
			}
		} else {
			throw new RedisException("无可用Redis连接!");
		}
	}

	public static void sign(RedisConnection redis, int system, int cluster) {
		try {
			RBucket<ChatBean> bucket = touch(redis);

			ChatBean bean = bucket.get();
			if (bean == null) {
				bean = new ChatBean();
			}
			bean.setSystem(system);
			bean.setCluster(cluster);
			bean.setInnertoken(RandomStringUtility.randomAlphaNumeric(32));

			bucket.set(bean);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public static ChatBean get(RedisConnection redis) {
		try {
			return touch(redis).get();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

}
